package campshop.daos;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import campshop.models.SchoolRegistered;
public class SchoolRegisteredDao extends JdbcDaoSupport{
	public long addSchoolRegistered(SchoolRegistered sr)
	{
		String sql = "insert into " +
				"tbl_school_registered (sr_sSchool, sr_dCreateDate)" +
				" values (?, ?)";
		if(super.getJdbcTemplate().update(sql, sr.getsSchoolName(), sr.getdCreateDate())>0){
			return super.getJdbcTemplate().queryForInt("select max(sr_uSchoolId) from tbl_school_registered");
		}else return 0L;
	}
	
	public boolean delSchoolRegistered(long uId)
	{
		String sql = "delete from tbl_school_registered where sr_uSchoolId = ?";
		if(super.getJdbcTemplate().update(sql, uId)>0) return true;
		return false;
	}

	public SchoolRegistered getSchoolRegistered(long uId)
	{
		String sql = "select * from tbl_school_registered where sr_uSchoolId=" + uId;
		return (SchoolRegistered)super.getJdbcTemplate().queryForObject(sql, new SchoolRegisteredRowMapper());
	}
	
	class SchoolRegisteredRowMapper implements RowMapper<SchoolRegistered>{
		@Override
		public SchoolRegistered mapRow(ResultSet rs, int rowNum) throws SQLException {
			SchoolRegistered sr = new SchoolRegistered();
			sr.setuSchoolId(rs.getLong("sr_uSchoolId"));
			sr.setsSchoolName(rs.getString("sr_sSchool"));
			sr.setdCreateDate(rs.getDate("sr_dCreateDate"));
			return sr;
		}	
	}
}